package health.lm.com.component_base.pay.alipay;

import android.app.Activity;

import com.alipay.sdk.app.PayTask;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2018/11/28
 * 邮箱：15206394364@163.com
 * 介绍：支付宝支付类
 * 修订：====================
 */
public class AliPayHelper {
    private static AliPayHelper aliPayHelper;
    private AliPayCallback mAliPayCallback;

    private AliPayHelper() {
    }

    public static AliPayHelper getInstance() {
        if (aliPayHelper == null) {
            aliPayHelper = new AliPayHelper();
        }
        return aliPayHelper;
    }

    public void pay(Activity activity, String orderInfo, AliPayCallback aliPayCallback) {
        this.mAliPayCallback = aliPayCallback;
        Observable.create((ObservableEmitter<Map<String, String>> emitter) -> {
            PayTask alipay = new PayTask(activity);
            Map<String, String> result = alipay.payV2(orderInfo, true);
            emitter.onNext(result);
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Map<String, String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Map<String, String> stringStringMap) {
                        PayResult payResult = new PayResult(stringStringMap);
                        mAliPayCallback.result(payResult);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 支付回调结果
     */
    public interface AliPayCallback {
        void result(PayResult payResult);
    }
}
